package com.luck.easyfoodbatch.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.GridImageAdapter;
import com.luck.easyfoodbatch.dialog.ShowProgressDialog;
import com.luck.easyfoodbatch.entity.FeedBack;
import com.luck.easyfoodbatch.mvp.mine.FeedBackPresenter;
import com.luck.easyfoodbatch.mvp.mine.FeedbackModel;
import com.luck.easyfoodbatch.mvp.mine.FeedbackView;
import com.luck.easyfoodbatch.mvp.mine.UploadImageModel;
import com.luck.easyfoodbatch.mvp.mine.UploadImageView;
import com.luck.easyfoodbatch.mvp.mine.UploadSinglePresenter;
import com.luck.easyfoodbatch.mvp.model.UploadModel;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.picture.lib.model.FunctionConfig;
import com.luck.picture.lib.model.PictureConfig;
import com.yalantis.ucrop.entity.LocalMedia;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class FeedbackActivity extends BaseActivity implements FeedbackView, UploadImageView {

    @BindView(R.id.right_title)
    TextView rightTitle;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    private FeedbackModel feedbackModel;
    private GridImageAdapter adapter;
    private UploadImageModel imageModel;
    private ShowProgressDialog dg;
    private List<LocalMedia> images = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback, "意见反馈", "提交", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        dg = new ShowProgressDialog(this);
        feedbackModel = new FeedBackPresenter(this, mContext);
        imageModel = new UploadSinglePresenter(this, mContext);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
        mLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycler.setLayoutManager(mLayoutManager);
        adapter = new GridImageAdapter(mContext, onAddPicClickListener);
        recycler.setAdapter(adapter);
    }

    private GridImageAdapter.onAddPicClickListener onAddPicClickListener = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick(int type, int position) {
            switch (type) {
                case 0:
                    int max = 5 - images.size();
                    FunctionConfig config = new FunctionConfig();
                    config.setCompress(true);
                    config.setMaxSelectNum(max);
                    PictureConfig.init(config);
                    PictureConfig.getPictureConfig().openPhoto(FeedbackActivity.this, resultCallback);
                    break;
                case 1:
                    if (images.size() > 0) {
                        images.remove(position);
                    }
                    adapter.notifyItemRemoved(position);
                    adapter.notifyDataSetChanged();
                    break;
            }
        }
    };


    @OnClick(R.id.right_title)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.right_title:
                String content = etContent.getText().toString().trim();
                if (!Tools.isNull(content)) {
                    dg.setContent("正在提交，请稍候……");
                    dg.show();
                    List<File> files = new ArrayList<>();
                    for (LocalMedia media : images) {
                        if (media.isCompressed()) {
                            files.add(new File(media.getCompressPath()));
                        } else {
                            files.add(new File(media.getPath()));
                        }
                    }
                    if (files.size() == 0) {
                        // 没图片 直接提交
                        feedbackModel.postSuggestion(content, "");
                    } else {
                        imageModel.uploads(1, null, files);
                    }
                } else {
                    T.showToast(mContext, "请输入您的宝贵意见~");
                }
                break;
        }
    }


    @Override
    public void submitFeedbackSuccess(FeedBack feedBack) {
        dg.dismiss();
        if (feedBack != null) {
            if (feedBack.getStatus() == HttpUtil.RESULT_OK) {
                T.showToast(mContext, feedBack.getMsg());
                finish();
            } else {
                T.showToast(mContext, feedBack.getMsg());
            }
        }
    }


    @Override
    public void uploadSuccess(UploadModel model) {
        if (model != null) {
            if (model.getStatus() == HttpUtil.RESULT_OK) {
                List<String> data = model.getData();
                if (data == null) {
                    data = new ArrayList<>();
                }
                String paths = Tools.tempImg(data);
                String content = etContent.getText().toString().trim();
                feedbackModel.postSuggestion(content, paths);
            }
        }
    }

    @Override
    public void httpStatusError() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void upProgress(long currentSize, long totalSize, float progress, long networkSpeed) {

    }


    private PictureConfig.OnSelectResultCallback resultCallback = new PictureConfig.OnSelectResultCallback() {
        @Override
        public void onSelectSuccess(List<LocalMedia> results) {
            if (images != null || images.size() > 0) {
                images.addAll(results);
                adapter.setPhotoInfos(images);
                adapter.notifyDataSetChanged();
            }
        }
    };

}
